#include "bluetooth.h"
#include <avr/io.h>
#include <avr/iom164.h> 
#include <util/delay.h>
#include <string.h>

void bluetooth_transmit (unsigned char data){
	
/*
*	Purpose: Transmit data to the Bluetooth Module
*	Input: Data to be transmitted
*	Output:	Nothing
*/

	while( !(UCSR1A & (1<<UDRE1))); 
	UDR1 = data;
}

unsigned char bluetooth_receive (void){

/*
*	Purpose: Receive data from the Bluetooth Module
*	Input: Nothing
*	Output:	Data received	
*/

	while (!(UCSR1A & (1<<RXC1)));
	return UDR1;
}	

void bluetooth_flush(void){

/*
*	Purpose: Clear all old data in the buffer
*	Input: Nothing
*	Output: Nothing
*/

	char tmp;
	while (UCSR1A & (1<<RXC1) ) tmp = UDR1;
}	

int bluetooth_sendcmd(unsigned char *in, char len_in){

/*
*	Purpose: Send a string to the Bluetooth module to print
*	Input:	in - Input buffer to be printed
*		len-in - Length of the buffer to be printed
*	Output: Error Codes
*/	

	int i;
	for(i=0;i<len_in;i++){
		bluetooth_transmit(in[i]);		
	}	
	return 0;
}

int bluetooth_init(){

/*
*	Purpose: Initialize the Bluetooth Device and check that communication is correct
*	Input: Nothing
*	Output: Returns OK or corresponding error codes
*/
	//Based on the RTADDR it can be determined if the baud rate and everything is correct
	//Also if it responds to this then we know then the state is 1
	PORTA = 0x00;	
	_delay_ms(1000);
	bluetooth_flush();	
	unsigned char in_buff[5] = "2";
	unsigned char out_buff[100];
	unsigned char error[5] = "ERROR";
	unsigned char check[5] = "\r\n1\r\n";	
	int result;
	int i;

	// Check if connection already made
	bluetooth_sendcmd(check, sizeof(check));
	char tmp;
	i = 0;	
	do{
		tmp = bluetooth_receive();
	}while(tmp!='\r');	// Wait for '/r'
	bluetooth_receive();	// Receive '/r'
	bluetooth_receive();	// Receive '/n'
	while(1){
		out_buff[i] = bluetooth_receive();
		if(i==100){
			return OVERFLOW;
		}
		if(out_buff[i] == '\r') break;
		i++;			
	}
	result = strncmp((char *)error, (char *)out_buff, sizeof(error)); // Is it error?
	if(result == 0){	// If so wait and listen for BTSTATE4 in bluetooth_listen
		PORTA = 0x01;
		result = bluetooth_listen();
		return result;	
	}
	result = strncmp((char *)in_buff, (char *)out_buff, sizeof(in_buff));
	if(result == 0){ 	// If not connection already made 
		return OK;
	}
	else{		
		bluetooth_flush();
		return BAD_DATA;
	}
}

int bluetooth_listen(){

/*
*	Purpose: Device listens and waits for a response from the Bluetooth Bee
*	Input: Nothing
*	Output: Returns OK or corresponding error codes
*/

	char state4[10] = "+BTSTATE:4";	// State when connection is made
	char response[100];		// Buffer for response
	int result = 0;			
	int not_connected = 1;		// Listening flag		
	PORTA = 0x01;
	int i;
	while(not_connected){
		unsigned char tmp;
		PORTA = 0x01;
		while(bluetooth_receive() != '\r');	// Wait for '\r'	
		bluetooth_receive(); // Receive '\n'
		for(i=0;i<100;i++){
			tmp = bluetooth_receive();
			if(tmp != '\r'){	// Wait for the ending '\r'
				response[i] = tmp;
			}
			else{
				break;
			}
		}
		result = strncmp(state4, response, sizeof(state4)); // Does it match state4?
		_delay_ms(20);
		if(result == 0){
			PORTA =0x02;
			_delay_ms(1000);	// Give Android time to set up
			bluetooth_flush();
			char response2[5];
			unsigned char cmd[5] = "\r\n1\r\n";
			unsigned char resp[5] = "\r\n2\r\n";
			result = 0;	
			bluetooth_sendcmd(cmd, sizeof(cmd));
			for(i=0;i<sizeof(response2);i++) response2[i] = bluetooth_receive();	
			result = strncmp((char *)resp, (char *)response2, sizeof(resp));	// Match ack?		
			if(result == 0){
				PORTA = 0x00;
				return OK;
			}
			else{
				PORTA = 0x03;
				return BAD_DATA;
			}			
		}	
	}	
	return 0;	
}





